import { request } from '@/utils/request'

// 注册
export const regAPI = ({ username, password, repassword }) => {
  return request({
    url: '/api/reg',
    method: 'post',
    data: {
      username,
      password,
      repassword
    }
  })
}

// 登录
export const loginAPI = ({ username, password }) => {
  return request({
    url: '/api/login',
    method: 'POST',
    data: {
      username,
      password
    }
  })
}

// 获取用户信息
export const getUserInfoAPI = () => {
  return request({
    url: '/my/userinfo',
    method: 'GET'
  })
}

// 获取菜单
export const menuAPI = () => {
  return request({
    url: '/my/menus',
    method: 'GET'
  })
}

// 修改用户信息
export const updateUserInfoAPI = ({ id, username, nickname, email, userPic = '' }) => {
  return request({
    url: '/my/userinfo',
    method: 'PUT',
    data: {
      id,
      username,
      nickname,
      email,
      user_pic: userPic
    }
  })
}

// 重置密码
export const updatePasswordAPI = ({ oldPwd, newPwd, rePwd }) => {
  return request({
    url: '/my/updatepwd',
    method: 'PUT',
    data: {
      old_pwd: oldPwd,
      new_pwd: newPwd,
      re_pwd: rePwd
    }
  })
}

// 获取文章分类
export const getArtCateListAPI = () => {
  return request({
    url: '/my/cate/list'
  })
}

// 添加文章分类
export const addArtCateAPI = ({ cateName, cateAlias }) => {
  return request({
    url: '/my/cate/add',
    method: 'POST',
    data: {
      cate_name: cateName,
      cate_alias: cateAlias
    }
  })
}
// 修改文章分类
export const updateArtCateAPI = ({ id, cateName, cateAlias }) => {
  return request({
    url: '/my/cate/info',
    method: 'PUT',
    data: {
      id,
      cate_name: cateName,
      cate_alias: cateAlias
    }
  })
}

// 删除文章分类
export const deleteArtCateAPI = id => {
  return request({
    url: '/my/cate/del',
    method: 'DELETE',
    params: {
      id
    }
  })
}

// 获取文章列表
export const getArtListAPI = ({ pagenum, pagesize, cateId, state }) => {
  return request({
    url: '/my/article/list',
    params: {
      pagenum,
      pagesize,
      cate_id: cateId || undefined,
      state
    }
  })
}

// 添加文章
export const addArticleAPI = (formData) => {
  return request({
    url: '/my/article/add',
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'form-data'
    }
  })
}

// 删除文章
export const getArticleByIdAPI = (id) => {
  return request({
    url: '/my/article/info',
    method: 'DELETE',
    params: {
      id
    }
  })
}

// 文章详情
export const delArticleAPI = (id) => {
  return request({
    url: '/my/article/info',
    params: {
      id
    }
  })
}
